package com.mf.all.controller.basic;


import com.mf.all.constant.ConstantBasic;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.utils.UtilController;
import com.mf.all.utils.UtilRedisClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.redisson.api.RLock;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoJob;
import com.mf.all.entity.req.basic.ReqJob;
import com.mf.all.service.ServiceJob;
import com.mf.all.entity.req.search.SearchReqJob;

import java.util.List;

/**
 * 岗位表
 *
 * @date 2025-04-02 22:36:46
 */
@RestController
@RequestMapping("job")
@Tag(name = "岗位表")
public class ControllerJob {
    @Resource
    private UtilRedisClient redisClient;
    @Resource
    private ServiceJob Servicejob;


    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("job:search")
    public Result<Page<DtoJob>> findPage(@RequestBody SearchReqJob searchReqJob) {
        return Result.success(Servicejob.findPageByParams(searchReqJob));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("job:search")
    public Result<List<DtoJob>> list(@RequestBody SearchReqJob searchReqJob) {
        return Result.success(Servicejob.findListByParams(searchReqJob));
    }

    @Operation(summary = "根据部门id查询岗位集合")
    @GetMapping("/listByDeptId")
    @SaCheckPermission("job:search")
    public Result<List<DtoJob>> listByDeptId(@RequestParam("deptId") Integer deptId) {
        return Result.success(Servicejob.listByDeptId(deptId));
    }


    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("job:save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqJob reqJob) {
        RLock lock = redisClient.getLock(ConstantBasic.JOB_CODE_PREFIX);

        try {
            lock.lock();
            Servicejob.saveJob(UtilController.setBo(reqJob, DtoJob.class));
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("job:update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqJob reqJob) {
        Servicejob.updateById(UtilController.setBo(reqJob, DtoJob.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @GetMapping("/delete")
    @SaCheckPermission("job:delete")
    public Result<Void> delete(@RequestParam("id") Integer id) {
        Servicejob.deleteById(id);
        return Result.success();
    }

}
